<template>
	<Form ref="form" :model="formData" label-position="top" :rules="rules">
		<FormDescItem label="库存下降设置" desc="设置库存下降时机，默认为当用户下单成功时商品库存下降">
			<Select v-model="formData.stock_change">
                <Option value="1">订单下单时减库存</Option>
                <Option value="2">订单付款时减库存</Option>
                <Option value="3">订单发货时减库存</Option>
            </Select>
		</FormDescItem>
		<FormDescItem label="订单自动取消时限" prop="order_cancel_time" desc="设置订单下单超过多长时间自动取消，单位为分钟，若设置为0则禁用">
			<Input v-model="formData.order_cancel_time" number placeholder="分钟" :disabled="formData.order_cancel_time_disabled">
				<Checkbox v-model="formData.order_cancel_time_disabled" slot="append">禁用</Checkbox>
			</Input>
		</FormDescItem>
		<FormDescItem label="运费计算方式">
			<RadioGroup v-model="formData.delivery_fee_type">
                <Radio label="0">累加计算</Radio>
                <Radio label="1">组合计算</Radio>
            </RadioGroup>
            <p slot="desc">设置运费模板计算方式，详细说明请查看： <a href="">《运费模板计费方式说明》</a></p>
		</FormDescItem>
		<FormDescItem label="商品详情页为你推荐" desc="开启后，商品详情页商品基本信息下方会根据用户行为展示用户可能喜欢的商品，提高商品成交转化">
			<RadioGroup v-model="formData.goods_recommend">
                <Radio label="1">开启</Radio>
                <Radio label="0">关闭</Radio>
            </RadioGroup>
		</FormDescItem>
		<FormDescItem label="立即购买按钮" desc="设置选择不展示，商品详情页仅展示加入购物车，不展示立即购买按钮">
			<RadioGroup>
                <Radio label="1">展示</Radio>
                <Radio label="0">不展示</Radio>
            </RadioGroup>
		</FormDescItem>
		<FormDescItem label="前台货币单位" desc="设置平台货币单位，默认为CNY(人民币)">
			<Select v-model="formData.currency">
                <Option v-for="item in currencyLists" :key="item.id" :value="item.id" :label="item.value">
                    <span>{{ item.value }}</span>
                    <span style="float:right;color:#ccc">{{ item.sign }}</span>
                </Option>
            </Select>
		</FormDescItem>
		<FormItem>
			<Button type="primary" :disabled="committing" @click="handleSubmit">{{committing ? '提交中...' : '保存设置'}}</Button>
		</FormItem>
	</Form>
</template>

<script>
import Util from '@/libs/util.js';
import FormDescItem from '@/components/form/FormDescItem.vue';
export default {
	components: {
		FormDescItem
	},
	data() {
		return {
			committing: false,
			formData: {
				stock_change: '1',
				order_cancel_time: null,
				order_cancel_time_disabled: false,
				delivery_fee_type: '1',
				goods_recommend: '1',
				currency: 1
			},
			rules: {
				order_cancel_time: [
					{type: 'number', message: "请填写数字类型", trigger: "blur"}
				]
			}
		}
	},
	computed: {
		configure() {
			return this.$store.state.config.configure;
		},
		currencyLists() {
        	return this.$store.state.config.currencyLists;
        }
	},
	mounted() {
		// 获取货币单位列表
		this.$store.dispatch('getCurrencyListsData');
		this.init();
	},
	watch: {
		configure: {
			deep: true,
			handler() {
				this.init();
			}
		}
	},
	methods: {
		init() {
			let configure = this.configure;
			if(configure && JSON.stringify(configure) != '{}') {
				this.formData.stock_change = configure.stock_change || '1';
				this.formData.currency = Number(configure.currency);
				this.formData.goods_recommend = configure.goods_recommend;
				if(configure.order_cancel_time == '0') {
					this.formData.order_cancel_time = null;
					this.formData.order_cancel_time_disabled = true;
				}else{
					this.formData.order_cancel_time = Number(configure.order_cancel_time) || 0;
				}
			}
		},
		handleSubmit() {
			if(this.committing) return false;
			this.$refs.form.validate(valid => {
                if(valid) {
                	let form = this.formData;
                	let params = {
                		stock_change: form.stock_change,
						delivery_fee_type: form.delivery_fee_type,
						goods_recommend: form.goods_recommend,
						currency: form.currency
                	}
                	if(form.order_cancel_time_disabled || !form.order_cancel_time) {
                		params.order_cancel_time = '0';
                	}else{
                		params.order_cancel_time = String(form.order_cancel_time);
                	}
                	this.committing = true;
                	Util.ajax.post(this.API['API_SETTING_SAVE'], {data: params}).then(res => {
                		this.committing = false;
                		this.$Message.success('保存成功');
                		// 同步本地数据
                		let configure = this.configure;
                		for(let k in params) {
                			configure[k] = params[k];
                		}
                		this.$store.commit('setSystemConfig', configure);
                	}).catch(() => {
                		this.committing = false;
                	})
                }
            });
		}
	}
}
</script>